package org.leon.config;

import org.leon.CustomerAnnotation;
import org.leon.repo.Animal;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringBootRunListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 根容器为Spring容器
        if(event.getApplicationContext().getParent()==null){
            Map<String,Object> beans = event.getApplicationContext().getBeansWithAnnotation(CustomerAnnotation.class);


            for (var bean : beans.entrySet()) {
                Animal animal = (Animal)bean.getValue();
                animal.speak();
            }
        }
    }
}
